<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>multipartformdata.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_gears_multipartformdata.js.html">multipartformdata.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2009 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview This class provides a builder for building multipart form data
<a name="line17"></a> * that is to be usef with Gears BlobBuilder and GearsHttpRequest.
<a name="line18"></a> *
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.gears.MultipartFormData&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line24"></a>goog.require(&#39;goog.gears&#39;);
<a name="line25"></a>goog.require(&#39;goog.string&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>/**
<a name="line30"></a> * Creates a new multipart form data builder.
<a name="line31"></a> * @constructor
<a name="line32"></a> */
<a name="line33"></a>goog.gears.MultipartFormData = function() {
<a name="line34"></a>  /**
<a name="line35"></a>   * The blob builder used to build the blob.
<a name="line36"></a>   * @type {GearsBlobBuilder}
<a name="line37"></a>   * @private
<a name="line38"></a>   */
<a name="line39"></a>  this.blobBuilder_ = goog.gears.getFactory().create(&#39;beta.blobbuilder&#39;);
<a name="line40"></a>
<a name="line41"></a>  /**
<a name="line42"></a>   * The boundary. This should be something that does not occurr in the values.
<a name="line43"></a>   * @type {string}
<a name="line44"></a>   * @private
<a name="line45"></a>   */
<a name="line46"></a>  this.boundary_ = &#39;----&#39; + goog.string.getRandomString();
<a name="line47"></a>};
<a name="line48"></a>
<a name="line49"></a>
<a name="line50"></a>/**
<a name="line51"></a> * Constant for a carriage return followed by a new line.
<a name="line52"></a> * @type {string}
<a name="line53"></a> * @private
<a name="line54"></a> */
<a name="line55"></a>goog.gears.MultipartFormData.CRLF_ = &#39;\r\n&#39;;
<a name="line56"></a>
<a name="line57"></a>
<a name="line58"></a>/**
<a name="line59"></a> * Constant containing two dashes.
<a name="line60"></a> * @type {string}
<a name="line61"></a> * @private
<a name="line62"></a> */
<a name="line63"></a>goog.gears.MultipartFormData.DASHES_ = &#39;--&#39;;
<a name="line64"></a>
<a name="line65"></a>
<a name="line66"></a>/**
<a name="line67"></a> * Whether the builder has been closed.
<a name="line68"></a> * @type {boolean}
<a name="line69"></a> * @private
<a name="line70"></a> */
<a name="line71"></a>goog.gears.MultipartFormData.prototype.closed_;
<a name="line72"></a>
<a name="line73"></a>
<a name="line74"></a>/**
<a name="line75"></a> * Whether the builder has any content.
<a name="line76"></a> * @type {boolean}
<a name="line77"></a> * @private
<a name="line78"></a> */
<a name="line79"></a>goog.gears.MultipartFormData.prototype.hasContent_;
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>/**
<a name="line83"></a> * Adds a Gears file to the multipart.
<a name="line84"></a> * @param {string} name The name of the value.
<a name="line85"></a> * @param {GearsFile} gearsFile The Gears file as returned from openFiles etc.
<a name="line86"></a> * @return {goog.gears.MultipartFormData} The form builder itself.
<a name="line87"></a> */
<a name="line88"></a>goog.gears.MultipartFormData.prototype.addFile = function(name, gearsFile) {
<a name="line89"></a>  return this.addBlob(name, gearsFile.name, gearsFile.blob);
<a name="line90"></a>};
<a name="line91"></a>
<a name="line92"></a>
<a name="line93"></a>/**
<a name="line94"></a> * Adds some text to the multipart.
<a name="line95"></a> * @param {string} name The name of the value.
<a name="line96"></a> * @param {*} value The value. This will use toString on the value.
<a name="line97"></a> * @return {goog.gears.MultipartFormData} The form builder itself.
<a name="line98"></a> */
<a name="line99"></a>goog.gears.MultipartFormData.prototype.addText = function(name, value) {
<a name="line100"></a>  this.assertNotClosed_();
<a name="line101"></a>
<a name="line102"></a>  // Also assert that the value does not contain the boundary.
<a name="line103"></a>  this.assertNoBoundary_(value);
<a name="line104"></a>
<a name="line105"></a>  this.hasContent_ = true;
<a name="line106"></a>  this.blobBuilder_.append(
<a name="line107"></a>      goog.gears.MultipartFormData.DASHES_ + this.boundary_ +
<a name="line108"></a>      goog.gears.MultipartFormData.CRLF_ +
<a name="line109"></a>      &#39;Content-Disposition: form-data; name=&quot;&#39; + name + &#39;&quot;&#39; +
<a name="line110"></a>      goog.gears.MultipartFormData.CRLF_ +
<a name="line111"></a>      // The BlobBuilder uses UTF-8 so ensure that we use that at all times.
<a name="line112"></a>      &#39;Content-Type: text/plain; charset=UTF-8&#39; +
<a name="line113"></a>      goog.gears.MultipartFormData.CRLF_ +
<a name="line114"></a>      goog.gears.MultipartFormData.CRLF_ +
<a name="line115"></a>      value +
<a name="line116"></a>      goog.gears.MultipartFormData.CRLF_);
<a name="line117"></a>  return this;
<a name="line118"></a>};
<a name="line119"></a>
<a name="line120"></a>
<a name="line121"></a>/**
<a name="line122"></a> * Adds a Gears blob as a file to the multipart.
<a name="line123"></a> * @param {string} name The name of the value.
<a name="line124"></a> * @param {string} fileName The name of the file.
<a name="line125"></a> * @param {GearsBlob} blob The blob to add.
<a name="line126"></a> * @return {goog.gears.MultipartFormData} The form builder itself.
<a name="line127"></a> */
<a name="line128"></a>goog.gears.MultipartFormData.prototype.addBlob = function(name, fileName,
<a name="line129"></a>                                                          blob) {
<a name="line130"></a>  this.assertNotClosed_();
<a name="line131"></a>
<a name="line132"></a>  this.hasContent_ = true;
<a name="line133"></a>  this.blobBuilder_.append(
<a name="line134"></a>      goog.gears.MultipartFormData.DASHES_ + this.boundary_ +
<a name="line135"></a>      goog.gears.MultipartFormData.CRLF_ +
<a name="line136"></a>      &#39;Content-Disposition: form-data; name=&quot;&#39; + name + &#39;&quot;&#39; +
<a name="line137"></a>      &#39;; filename=&quot;&#39; + fileName + &#39;&quot;&#39; +
<a name="line138"></a>      goog.gears.MultipartFormData.CRLF_ +
<a name="line139"></a>      &#39;Content-Type: application/octet-stream&#39; +
<a name="line140"></a>      goog.gears.MultipartFormData.CRLF_ +
<a name="line141"></a>      goog.gears.MultipartFormData.CRLF_);
<a name="line142"></a>  this.blobBuilder_.append(blob);
<a name="line143"></a>  this.blobBuilder_.append(goog.gears.MultipartFormData.CRLF_);
<a name="line144"></a>  return this;
<a name="line145"></a>};
<a name="line146"></a>
<a name="line147"></a>
<a name="line148"></a>/**
<a name="line149"></a> * The content type to set on the GearsHttpRequest.
<a name="line150"></a> *
<a name="line151"></a> *   var builder = new MultipartFormData;
<a name="line152"></a> *   ...
<a name="line153"></a> *   ghr.setRequestHeader(&#39;Content-Type&#39;, builder.getContentType());
<a name="line154"></a> *   ghr.send(builder.getAsBlob());
<a name="line155"></a> *
<a name="line156"></a> * @return {string} The content type string to be used when posting this with
<a name="line157"></a> *   a GearsHttpRequest.
<a name="line158"></a> */
<a name="line159"></a>goog.gears.MultipartFormData.prototype.getContentType = function() {
<a name="line160"></a>  return &#39;multipart/form-data; boundary=&#39; + this.boundary_;
<a name="line161"></a>};
<a name="line162"></a>
<a name="line163"></a>
<a name="line164"></a>/**
<a name="line165"></a> * @return {GearsBlob} The blob to use in the send method of the
<a name="line166"></a> *     GearsHttpRequest.
<a name="line167"></a> */
<a name="line168"></a>goog.gears.MultipartFormData.prototype.getAsBlob = function() {
<a name="line169"></a>  if (!this.closed_ &amp;&amp; this.hasContent_) {
<a name="line170"></a>    this.blobBuilder_.append(
<a name="line171"></a>        goog.gears.MultipartFormData.DASHES_ +
<a name="line172"></a>        this.boundary_ +
<a name="line173"></a>        goog.gears.MultipartFormData.DASHES_ +
<a name="line174"></a>        goog.gears.MultipartFormData.CRLF_);
<a name="line175"></a>    this.closed_ = true;
<a name="line176"></a>  }
<a name="line177"></a>  return this.blobBuilder_.getAsBlob();
<a name="line178"></a>};
<a name="line179"></a>
<a name="line180"></a>
<a name="line181"></a>/**
<a name="line182"></a> * Asserts that we do not try to add any more data to a closed multipart form
<a name="line183"></a> * builder.
<a name="line184"></a> * @throws {Error} If the multipart form data has already been closed.
<a name="line185"></a> * @private
<a name="line186"></a> */
<a name="line187"></a>goog.gears.MultipartFormData.prototype.assertNotClosed_ = function() {
<a name="line188"></a>  goog.asserts.assert(!this.closed_, &#39;The multipart form builder has been &#39; +
<a name="line189"></a>                      &#39;closed and no more data can be added to it&#39;);
<a name="line190"></a>};
<a name="line191"></a>
<a name="line192"></a>/**
<a name="line193"></a> * Asserts that the value does not contain the boundary.
<a name="line194"></a> * @param {*} v The value to ensure that the string representation does not
<a name="line195"></a> *     contain the boundary token.
<a name="line196"></a> * @throws {Error} If the value contains the boundary.
<a name="line197"></a> * @private
<a name="line198"></a> */
<a name="line199"></a>goog.gears.MultipartFormData.prototype.assertNoBoundary_ = function(v) {
<a name="line200"></a>  goog.asserts.assert(String(v).indexOf(this.boundary_) == -1,
<a name="line201"></a>                      &#39;The value cannot contain the boundary&#39;);
<a name="line202"></a>};
</pre>


</body>
</html>
